<script lang="ts" setup name="Goods">
// 商品详情
import GoodsImage from './components/goods-image.vue'
import GoodsSales from './components/goods-sales.vue'
import GoodsName from './components/goods-name.vue'
import GoodsSku from './components/goods-sku.vue'
import GoodsDetail from './components/goods-detail.vue'
import GoodsHot from './components/goods-hot.vue'
import useStore from '@/store';
import { storeToRefs } from 'pinia';
import { watchEffect, onBeforeUnmount, ref } from 'vue';
import { useRoute } from 'vue-router'
import Message from '@/components/message'
import { CartItem } from '@/types/cart'
const route = useRoute()
const { goods, cart } = useStore()
watchEffect(() => {
  if (route.fullPath === `/goods/${route.params.id}`) {
    // goods.resetGoodsInfo() // 重置商品信息
    goods.getGoodsInfo(route.params.id as string)
  }
})
const { info } = storeToRefs(goods)
// 组件销毁之前
onBeforeUnmount(() => {
  goods.resetGoodsInfo() // 重置商品信息
})
// 记录选中的skuId
const currentSkuId = ref('')
// 父组件根据skuId 商品的渲染
const changeSkuFn = (skuId: string) => {
  // console.log(skuId);
  const sku = info.value.skus.find(item => item.id === skuId)
  currentSkuId.value = skuId
  if (sku) {
    // 更新库存
    info.value.inventory = sku.inventory
    // 更新现价
    info.value.price = sku.price
    // 更新原价
    info.value.oldPrice = sku.oldPrice
  }
}
// ---------------------------
// 商品的数量 - 默认值
const count = ref(1)
// 加入购物车
const addCart =async () => {
  // 判断是否选中了完整的规格属性 -> 是否是一个完整的sku
  if (!currentSkuId.value) {
    return Message.warning('请选择完整规格信息')
  }
  // 查找当前添加商品的sku
  const sku = info.value.skus.find(item => item.id === currentSkuId.value)!
  const attrText = sku.specs.map(item => item.name + ':' +  item.valueName).join(' ')

  // 发送请求 - 将商品加入到购物车
  await cart.addCart({
    // 本地添加
    id: info.value.id,
    name: info.value.name,
    picture: info.value.mainPictures[0],
    price: info.value.price,
    count: count.value,
    skuId: currentSkuId.value,
    attrsText: attrText,
    selected: true,
    nowPrice: info.value.price,
    stock: info.value.inventory,
    isEffective: true,
  } as CartItem)
  Message.success('添加成功~')
}

</script>
<template>
  <div class="xtx-goods-page">
    <div class="container">
      <template v-if="info.categories">
        <!-- 面包屑 -->
        <XtxBread>
          <XtxBreadItem to="/">首页</XtxBreadItem>
          <!-- 一级分类 -->
          <XtxBreadItem :to="`/category/${info.categories[1].id}`">{{ info.categories[1].name }}</XtxBreadItem>
          <!-- 二级分类 -->
          <XtxBreadItem :to="`/category/${info.categories[0].id}`">{{ info.categories[0].name }}</XtxBreadItem>
          <XtxBreadItem to="/">{{ info.name }}</XtxBreadItem>
        </XtxBread>

        <!-- 商品信息 -->
        <div class="goods-info">
          <div class="media">
            <GoodsImage :images="info.mainPictures"></GoodsImage>
            <GoodsSales></GoodsSales>
          </div>
          <div class="spec">
            <GoodsName :goods="info"></GoodsName>
            <!-- 规格组件 -->
            <!-- skuId="1369155864430120962" -->
            <GoodsSku :goods="info" @changeSku="changeSkuFn"></GoodsSku>
            <!-- 数字选择框 -->
            <!-- min 1  max 限购 / 库存 -->
            <XtxNumbox v-model="count" showLabel></XtxNumbox>
            <!-- 通用的按钮组件 -->
            <!-- size 尺寸/大小 -->
            <!-- type 颜色 -->
            <XtxButton
              type="primary"
              @click="addCart"
              style="margin-top: 20px"
            >
              加入购物车
            </XtxButton>
          </div>
        </div>


        <!-- 商品详情 -->
        <div class="goods-footer">
          <div class="goods-article">
            <!-- 商品+评价 -->
            <div class="goods-tabs">
              <!-- 商品详情 -->
              <GoodsDetail :goods="info"></GoodsDetail>
            </div>
          </div>
          <!-- 24热榜+专题推荐 -->
          <div class="goods-aside">
            <GoodsHot :type="1" />
            <GoodsHot :type="2" />
            <GoodsHot :type="3" />
          </div>
        </div>
      </template>

      <!-- 占位的 -->
      <div v-else>
        <!-- 面包屑 -->
        <XtxBread>
          <XtxBreadItem to="/">首页</XtxBreadItem>
          <XtxBreadItem to="/">xxx</XtxBreadItem>
          <XtxBreadItem to="/">xxx</XtxBreadItem>
          <XtxBreadItem to="/">xxx</XtxBreadItem>
        </XtxBread>
        <!-- 商品信息 -->
        <div class="goods-info"></div>
        <!-- 商品详情 -->
        <div class="goods-footer">
          <div class="goods-article">
            <!-- 商品+评价 -->
            <div class="goods-tabs"></div>
          </div>
          <!-- 24热榜+专题推荐 -->
          <div class="goods-aside"></div>
        </div>
      </div>
    </div>
  </div>
</template>

<style scoped lang="less">
.goods-info {
  min-height: 600px;
  background: #fff;
  display: flex;

  .media {
    width: 580px;
    height: 600px;
    padding: 30px 50px;
  }

  .spec {
    flex: 1;
    padding: 30px 30px 30px 0;
  }
}

.goods-footer {
  display: flex;
  margin-top: 20px;

  .goods-article {
    width: 940px;
    margin-right: 20px;
  }

  .goods-aside {
    width: 280px;
    min-height: 1000px;
  }
}

.goods-tabs {
  min-height: 600px;
  background: #fff;
}

.goods-warn {
  min-height: 600px;
  background: #fff;
  margin-top: 20px;
}
</style>